package top.anka1.fxgui;

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;

import javafx.scene.control.cell.PropertyValueFactory;
import top.anka1.ims.*;
import top.anka1.ims.RestaurantManagementSystem.Ingredient;

import java.util.ArrayList;
/**
 * DisplayIngredientsController is a controller class for displaying ingredients in a table view.
 * It sets up the table columns and populates the table with ingredient data from the restaurant service.
 * @author Anka
 */
public class DisplayIngredientsController {
    private RestaurantGUI restaurantGUI;
    @FXML
    private TableView<Ingredient> ingredientsTable;
    @FXML
    private TableColumn<Ingredient, String> nameColumn;
    @FXML
    private TableColumn<Ingredient, Double> priceColumn;
    public void setData(RestaurantGUI restaurantGUI) {
        this.restaurantGUI = restaurantGUI;
        nameColumn.setCellValueFactory(new PropertyValueFactory<>("name"));
        nameColumn.setStyle("-fx-alignment: CENTER;");
        priceColumn.setCellValueFactory(new PropertyValueFactory<>("price"));
        priceColumn.setStyle("-fx-alignment: CENTER;");

        priceColumn.setCellFactory(column -> new javafx.scene.control.cell.TextFieldTableCell<>(
                new javafx.util.converter.DoubleStringConverter() {
                    @Override
                    public String toString(Double value) {
                        if (value == null) return "";
                        return String.format("¥%.2f", value);
                    }
                }
        ));

        ArrayList<Ingredient> ingredientList = new ArrayList<>();
        for (String name : restaurantGUI.getRestaurantService().getIngredientInventory().keySet()) {
            Ingredient ingredient = restaurantGUI.getRestaurantService().getIngredientInventory().get(name);
            ingredientList.add(ingredient);
        }
        ObservableList<Ingredient> ingredients = FXCollections.observableArrayList(ingredientList);
        ingredientsTable.setItems(ingredients);
    }
}
